﻿using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RsCms.Application.User.Dto;
using RsCode;
using RsCode.AspNetCore.Jwt;
using Rswl.Common.Application;
using Rswl.Common.Application.User.Dto;
using Rswl.Common.Core;
using Rswl.Common.Core.User.Aggregate.ValueObject;

namespace Plugin.User.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AdminController : ControllerBase
    {
        
        IUserAppService userService;
        IUserDomainService userDomainService;
        public AdminController(IUserAppService _userService, IUserDomainService userDomainService)
        {
            userService = _userService;
            this.userDomainService = userDomainService;
        } 
        

        //更换角色
        [Authorize("admin")]
        [HttpPost("role")]
        public async Task<object> ChangeRoleAsync(RoleChangeDto dto)
        {
            await userService.ChangeRoleAsync(dto);
            return "Ok";
        }

        //查看所有用户信息
        [Authorize("admin")]
        [HttpGet("user/list")]
        public async Task<PageData<UserInfoDto>> PageUserAsync(int page, int pagesize)
        {
            return await userService.PageUserInfoAsync(page, pagesize);
        }

        //登录
        [HttpPost("login")]
        public async Task<AccessTokenInfo> LoginAsync(PasswordLoginDto dto)
        { 
          return await  userDomainService.AdminLoginAsync(dto.Adapt<PasswordLoginValueObject>());
        }

        [Authorize("admin")]
        [HttpPost("create/account")]
        public async Task<object>CreateAccountAsync(AccountCreateDto dto)
        {
            await userService.AdminCreateAccountAsync(dto);
            return "操作成功";
        }

        //启用与关闭帐号
        [Authorize("admin")]
        [HttpPost("account/status")]
        public async Task<object>AccountStatusAsync(AccountStatusEditDto dto)
        {
            await userService.ChangeAccountStatusAsync(dto);
            return "ok";
        }
    }
}
